package cc.rengu.redp.bizimpl.csmc.controller;


import cc.rengu.redp.bizimpl.bean.LabelValueBean;
import cc.rengu.redp.bizimpl.constant.UpmpConstant;
import cc.rengu.redp.bizimpl.csmc.domain.ReconPathInfo;
import cc.rengu.redp.bizimpl.csmc.domain.ReconSceneStat;
import cc.rengu.redp.bizimpl.csmc.service.IReconPathInfoService;
import cc.rengu.redp.bizimpl.csmc.service.IReconSceneStatService;
import cc.rengu.redp.bizimpl.csmc.vo.ReconPathInfoVO;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpSysParam;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author guohaocong
 */
@Slf4j
@RestController
@Api(tags = "统一对账管理：对账链路配置管理")
@RequestMapping("/admin/csmc/recon-path-info")
public class ReconPathInfoController extends BaseController {

    @Autowired
    private IReconPathInfoService reconPathInfoService;
    @Autowired
    private IUpmpSysParamService upmpSysParamService;
    @Autowired
    private IReconSceneStatService reconSceneStatService;


    @Log("分页")
    @GetMapping
    public Map<String, Object> getPageList(QueryRequest queryRequest, ReconPathInfoVO reconPathInfoVO) throws RedpException{
        return getDataTable(reconPathInfoService.findByPage(queryRequest, reconPathInfoVO));
    }

    @Log("获取对账场景列表")
    @GetMapping("/reconSceneIdList")
    public List<LabelValueBean> getReconSceneIdList(ReconPathInfoVO reconPathInfoVO){
        UpmpSysParam defaultInst = upmpSysParamService.getById(UpmpConstant.DEAFULT_INST_PARAM_ID);
        LambdaQueryWrapper<ReconSceneStat> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.and(qw->qw.eq(ReconSceneStat::getInstId,defaultInst.getInstId())
                                .likeRight(ReconSceneStat::getReconSceneId,reconPathInfoVO.getReconSceneId()));
        List<ReconSceneStat> list = reconSceneStatService.list(queryWrapper);
        List<LabelValueBean> result = Lists.newArrayList();
        LabelValueBean firt = new LabelValueBean("", "请选择");
        result.add(firt);
        list.forEach(inst -> {
            //                                                  label                         value
            LabelValueBean labelValueBean = new LabelValueBean(UUID.randomUUID().toString().substring(5), inst.getReconSceneId());
            result.add(labelValueBean);
        });
        return result;

    }

    /**
     * 模糊查询，查找对账场景列表
     * @param
     * @throws RedpException
     */
    @Log("获取对账场景列表")
    @GetMapping("reconSceneIdList2")
    public Map<String,Object>  reconSceneIdList2(ReconSceneStat reconSceneStat, QueryRequest request)throws RedpException{
//        return reconPathInfoService.getReconSceneIdList2(reconSceneId,request);
        return getDataTable(reconPathInfoService.getReconSceneIdList(reconSceneStat,request));
    }

    @Log("新增")
    @PostMapping
    //    @RequiresPermissions("reconPathInfo:add)
    public void addReconPathInfo(@Valid ReconPathInfoVO reconPathInfoVO) throws RedpException {
      try{
          ReconSceneStat reconSceneStat = reconSceneStatService.ByReconSceneId(reconPathInfoVO.getInstId(), reconPathInfoVO.getReconSceneId());
          ReconPathInfo reconPathInfo = reconPathInfoService.ByTxnChainCode(reconPathInfoVO.getTxnChainCode(), reconPathInfoVO.getInstId());
          String message ="";
          if (reconSceneStat == null){
              message = "所选对账场景非正式数据，需前往审核界面进行审核!";
              log.error(message);
              throw new RedpException(message);
          }
          if(reconPathInfo != null){
              message = "您输入的数据已经存在！请重新输入";
              log.error(message);
              throw new RedpException(message);
          }
            this.reconPathInfoService.createReconPath(reconPathInfoVO);
        } catch (Exception e) {
            String message = e.getMessage();
            log.error(message, e);
            throw new RedpException(message);
        }

    }

    @Log("修改")
    @PutMapping
    //    @RequiresPermissions("reconPathInfo:update")
    public void updateReconPathInfo(ReconPathInfoVO reconPathInfoVO) throws RedpException {
        try{
            ReconSceneStat reconSceneStat = reconSceneStatService.ByReconSceneId(reconPathInfoVO.getInstId(), reconPathInfoVO.getReconSceneId());
            if (reconSceneStat == null){
                String message = "所选对账场景非正式数据，需前往审核界面进行审核!";
                log.error(message);
                throw new RedpException(message);
            }
            this.reconPathInfoService.updateReconPath(reconPathInfoVO);
        } catch (Exception e) {
            String message = e.getMessage();
            log.error(message, e);
            throw new RedpException(message);
        }

    }

    @Log("修改状态")
    @PutMapping("/check")
    public void updateStatusReconPathInfo(ReconPathInfoVO reconPathInfoVO) throws RedpException {
        try{
            this.reconPathInfoService.updateReconPath(reconPathInfoVO);
        } catch (Exception e) {
            String message = e.getMessage();
            log.error(message, e);
            throw new RedpException(message);
        }

    }


}
